/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import java.io.IOException;

import com.Arkham.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class GameMenu extends Activity implements OnClickListener 
{
	public  ViewCore GLView;

	TextView title, time;
	MediaPlayer saveSound;						//Sound

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		saveSound = MediaPlayer.create(this, R.raw.right_sound);		//Set sound effect for correct entry

		//removes title bar and sets the theme
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

		super.onCreate(savedInstanceState);
		setContentView(com.Arkham.R.layout.gamemenu);

		View classroomButton = findViewById(com.Arkham.R.id.classroom_button);
		classroomButton.setOnClickListener(this);  
		View courtyardButton = findViewById(com.Arkham.R.id.courtyard_button);
		courtyardButton.setOnClickListener(this);
		View storeButton = findViewById(com.Arkham.R.id.store_button);
		storeButton.setOnClickListener(this);  
		View inventoryButton = findViewById(com.Arkham.R.id.inventory_button);
		inventoryButton.setOnClickListener(this);    
		View partyButton = findViewById(com.Arkham.R.id.party_button);
		partyButton.setOnClickListener(this);  
		View saveButton = findViewById(com.Arkham.R.id.save_button);
		saveButton.setOnClickListener(this); 
		View exitButton = findViewById(com.Arkham.R.id.exit_button);
		exitButton.setOnClickListener(this); 

		title = (TextView) findViewById(R.id.schoolHouse_Text);
		title.setText("School House");
		time = (TextView) findViewById(R.id.school_time);

		//check to see what time it should be.
		if(GameFile.storyFlag[0] == true)
			time.setText("Lunch Period");
		else
			time.setText("First Period");

	}

	public void onClick(View v) {
		Intent i;
		switch (v.getId())
		{
		case com.Arkham.R.id.exit_button:
			finish();
			break;
		case com.Arkham.R.id.classroom_button:
			i = new Intent(this, Classroom.class);
			startActivity(i);
			break;
		case com.Arkham.R.id.courtyard_button:
			if ( GameFile.storyFlag[0] == false ){
				i = new Intent(this, PopUpmsg.class);
				i.putExtra("whosays", "There is a sign posted on the door");
				i.putExtra("says", Dialogue.CourtyardClosedTillLunch());
				startActivity(i);
			}
			else if ( GameFile.storyFlag[1] == true ){
				setUpRandomBattle();
				i = new Intent(this, Battle.class);
				startActivity(i);
			}
			else {
				setUpIntroBattle();
				i = new Intent(this, Battle.class);
				startActivity(i);				
			}
			break;
		case com.Arkham.R.id.store_button:
			if ( GameFile.storyFlag[0] == false ){
				i = new Intent(this, PopUpmsg.class);
				i.putExtra("whosays", "There is a sign posted on the door");
				i.putExtra("says", Dialogue.StoreClosedTillLunch());
				startActivity(i);
			}
			else{
				i = new Intent(this, Shop.class);
				startActivity(i);
			}
			break;
		case com.Arkham.R.id.inventory_button:
			i = new Intent(this, Backpack.class);
			startActivity(i);
			break;
		case com.Arkham.R.id.party_button:
			i = new Intent(this, CharacterView.class);
			startActivity(i);
			break;
		case com.Arkham.R.id.save_button:
			GameFile game = new GameFile();

			try 
			{
				if(saveSound != null)
					saveSound.start();
				game.saveGameToFile();
			} 
			catch (IOException e) 
			{e.printStackTrace();}
			break;
		}
	}
	
	public void setUpIntroBattle(){
		///////////////////////////
		//Set up the intro battle//
		///////////////////////////
		BattleInfo.resetParties();
		//create and add battle map
		boolean[][] battleMap = new boolean[15][15];
		for(int w=0; w<15; w++)
			for(int h=0; h<15; h++)
				battleMap[w][h] = false;
		BattleInfo.setBattleMap(battleMap);
		//Add main character to battle
		BattleInfo.addGoodCharacterToParty(GameFile.party.characters[0], 2, 7);
		//create three enemies with sprites
		Character Boss = new Character();
		Character Minion1 = new Character();
		Character Minion2 = new Character();
		Boss.setSpriteId(com.Arkham.R.drawable.mage);
		Minion1.setSpriteId(com.Arkham.R.drawable.ninja1);
		Minion2.setSpriteId(com.Arkham.R.drawable.ninja2);
		Actor BossSprite = new Actor(2,2, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.mage), 128,128);
		Actor M1Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja1), 64,64);
		Actor M2Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja2), 64,64);
		Boss.RandomizeMonster(2, "Matt", BossSprite);
		Minion1.RandomizeMonster(1, "Minion1, Purple Ninja", M1Sprite);
		Minion2.RandomizeMonster(1, "Minion2, Black Ninja", M2Sprite);
		Boss.equipItem(Armory.Pencil(), Constants.R_HAND);
		Minion1.equipItem(Armory.Pencil(), Constants.R_HAND);
		Minion2.equipItem(Armory.Pencil(), Constants.R_HAND);
		Boss.equipItem(Armory.UsedEraser(), Constants.L_HAND);
		Minion1.equipItem(Armory.UsedEraser(), Constants.L_HAND);
		Minion2.equipItem(Armory.UsedEraser(), Constants.L_HAND);
		Boss.equipItem(Armory.StreetCloths(), Constants.BODY);
		Boss.equipItem(Armory.DunceCap(), Constants.HEAD);
		//Add three enemies to battle
		BattleInfo.addBadCharacterToParty(Minion1, 11, 10);
		BattleInfo.addBadCharacterToParty(Minion2, 11, 4);
		BattleInfo.addBadCharacterToParty(Boss, 13, 7);
		/////////////////////////
		//finished battle setup//
		/////////////////////////
	}
	
	public void setUpRandomBattle(){
		int startingX;
		int startingY;
		int pos;
		int highestLvl = 0;
		BattleInfo.resetParties();
		//create and add battle map
		boolean[][] battleMap = new boolean[15][15];
		for(int w=0; w<15; w++)
			for(int h=0; h<15; h++)
				battleMap[w][h] = false;
		BattleInfo.setBattleMap(battleMap);
		//Add main character to battle
		pos = 0;
		while(GameFile.party.characters[pos].getName() != ""){
			startingX = (int)(Math.random()* 4);
			startingY = (int)(Math.random()* 14);
			BattleInfo.addGoodCharacterToParty(GameFile.party.characters[pos], startingX, startingY);
			if(GameFile.party.characters[pos].getLevel() > highestLvl){
				highestLvl = GameFile.party.characters[pos].getLevel();
			}
			pos++;
		}
		
		Character Enemy;
		int LL = (int) Math.ceil(highestLvl / 3);
		int HL = LL * 2;
		int numE = 1;
		pos = 0;
		while(pos < numE){
			Enemy = new Character();
			Enemy.RandomizeMonster(HL, ("Club Leader" + (pos+1)), 
					new Actor(2,2, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.mage), 128,128));
			Enemy.setSpriteId(com.Arkham.R.drawable.mage);
			startingX = 14 -(int)(Math.random()* 4);
			startingY = (int)(Math.random()* 14);
			BattleInfo.addBadCharacterToParty(Enemy, startingX, startingY);
			pos++;
		}
		
		
		numE += BattleInfo.Good.size();
		pos = 0;
		while(pos < numE){
			Enemy = new Character();
			if(pos%2 == 0){
			   Enemy.RandomizeMonster(LL, ("Club Member" + (pos+1)), 
					   new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja1), 64,64));
			   Enemy.setSpriteId(com.Arkham.R.drawable.ninja1);
			}
			else{
				   Enemy.RandomizeMonster(LL, ("Club Member" + (pos+1)), 
						   new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja2), 64,64));
				   Enemy.setSpriteId(com.Arkham.R.drawable.ninja2);
			}
			startingX = 14 - (int)(Math.random()* 7);
			startingY = (int)(Math.random()* 14);
			BattleInfo.addBadCharacterToParty(Enemy, startingX, startingY);
			pos++;
		}
		
		
	}
}